﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using Bots.Index;

namespace Sample
{
    public partial class AddForm : Form
    {
        private long linkid = 0;

        public AddForm()
        {
            InitializeComponent();
        }

        public AddForm(long id):this() {
            this.linkid = id;
        }

        protected override void OnLoad(EventArgs e)
        {
            SqlConnection connection = null;
            if (this.linkid > 0) {
                connection = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString);
                SqlCommand cmd = new SqlCommand("select * from link where id=@linkid", connection);
                connection.Open();
                cmd.Parameters.AddWithValue("@linkid", this.linkid);
                SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                while (reader.Read()) {
                    titleBox.Text = reader.GetString(1);
                    urlBox.Text = reader.GetString(2);
                    descBox.Text = reader.GetString(3);
                    break;
                }
                connection.Close();
            }
            base.OnLoad(e);
        }

        private void okBtn_Click(object sender, EventArgs e)
        {
            if (linkid > 0)
            {
                UpdateIndex(this.linkid, titleBox.Text, urlBox.Text, descBox.Text, DateTime.Now);
            }
            else {
                InsertIndex(titleBox.Text, urlBox.Text, descBox.Text, DateTime.Now);
            }
        }

        private void InsertIndex(string title, string url, string desc, DateTime createdate) {
            SqlConnection connection = null;
            try
            {
                connection = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString);
                SqlCommand cmd = new SqlCommand("AddLink", connection);
                connection.Open();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(CreateInputParam("@Title", SqlDbType.NVarChar, title));
                cmd.Parameters.Add(CreateInputParam("@Url", SqlDbType.NVarChar, url));
                cmd.Parameters.Add(CreateInputParam("@Description", SqlDbType.NVarChar, desc));
                cmd.Parameters.Add(CreateInputParam("@CreateDate", SqlDbType.DateTime, createdate));
                SqlParameter p1 = new SqlParameter("@LinkID", SqlDbType.BigInt);
                p1.Direction = ParameterDirection.Output;
                cmd.Parameters.Add(p1);
                int ret = cmd.ExecuteNonQuery();

                //add incremental index
                Link link = new Link(Convert.ToInt64(p1.Value));
                link.Title = title;
                link.Url = url;
                link.Description = desc;
                link.ObjectStatue = Bots.ObjectStatue.New;
                IncIndexManager.IncIndexAdd(link);
            }
            finally
            {
                if (connection != null)
                {
                    connection.Close();
                }
            }
        }

        private void UpdateIndex(long linkid, string title, string url, string desc, DateTime createdate) {
            SqlConnection connection = null;
            try
            {
                connection = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString);
                SqlCommand cmd = new SqlCommand("UpdateLink", connection);
                connection.Open();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(CreateInputParam("@LinkId", SqlDbType.BigInt, linkid));
                cmd.Parameters.Add(CreateInputParam("@Title", SqlDbType.NVarChar, title));
                cmd.Parameters.Add(CreateInputParam("@Url", SqlDbType.NVarChar, url));
                cmd.Parameters.Add(CreateInputParam("@Description", SqlDbType.NVarChar, desc));
                cmd.Parameters.Add(CreateInputParam("@CreateDate", SqlDbType.DateTime, createdate));
                int ret = cmd.ExecuteNonQuery();

                Link link = new Link(linkid);
                link.Title = title;
                link.Url = url;
                link.Description = desc;
                link.CreateDateTime = createdate;
                link.ObjectStatue = Bots.ObjectStatue.Update;
                IncIndexManager.IncIndexAdd(link);
            }
            finally
            {
                if (connection != null)
                {
                    connection.Close();
                }
            }
        }

        private SqlParameter CreateInputParam(string paramName, SqlDbType dbType, object objValue)
        {
            SqlParameter parameter1 = new SqlParameter(paramName, dbType);
            if (objValue == null)
            {
                parameter1.IsNullable = true;
                parameter1.Value = DBNull.Value;
                return parameter1;
            }
            parameter1.Value = objValue;
            return parameter1;
        }
    }
}
